<!-- 用户信息 -->
<template>
  <s-layout title="学员信息" class="set-userinfo-wrap">
    <uni-forms :model="state.model" :rules="state.rules" labelPosition="left" border class="form-box">
      <!-- 头像 -->
      <view class="ss-flex ss-row-center ss-col-center ss-p-t-60 ss-p-b-0 bg-white">
        <view class="header-box-content">
          <su-image class="content-img" isPreview :current="0" :src="state.model.avatar" :height="160" :width="160"
            :radius="80" mode="aspectFill" />
          <view class="avatar-action" v-if="!isEdit">
            <!-- #ifdef MP -->
            <button class="ss-reset-button avatar-action-btn" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
              修改
            </button>
            <!-- #endif -->
            <!-- #ifndef MP -->
            <button class="ss-reset-button avatar-action-btn" @tap="onChangeAvatar">修改</button>
            <!-- #endif -->
          </view>
        </view>
      </view>

      <view class="bg-white ss-p-x-30">
        <!-- 昵称 + 性别 -->
        <uni-forms-item name="name" label="姓名" required>
          <uni-easyinput v-model="state.model.name" type="name" :disabled="isEdit" :trim="true" placeholder="姓名"
            :inputBorder="false" :placeholderStyle="placeholderStyle" />
        </uni-forms-item>
        <uni-forms-item name="sex" label="性别" required>
          <view class="ss-flex ss-col-center ss-h-100">
            <radio-group @change="onChangeGender" class="ss-flex ss-col-center">
              <label class="radio" v-for="item in sexRadioMap" :key="item.value">
                <view class="ss-flex ss-col-center ss-m-r-32">
                  <radio :value="item.value" color="var(--ui-BG-Main)" style="transform: scale(0.8)" :disabled="isEdit"
                    :checked="parseInt(item.value) === state.model.sex" />
                  <view class="gender-name">{{ item.name }}</view>
                </view>
              </label>
            </radio-group>
          </view>
        </uni-forms-item>

        <uni-forms-item name="idCard" label="身份证" required>
          <uni-easyinput v-model="state.model.idCard" type="idCard" :disabled="isEdit" :trim="true" maxlength="18"
            placeholder="身份证号码" :inputBorder="false" :placeholderStyle="placeholderStyle" />
        </uni-forms-item>

        <uni-forms-item name="mobile" label="手机号" required>
          <uni-easyinput v-model="state.model.mobile" placeholder="手机号码" :disabled="isEdit" maxlength="11" :trim="true"
            :inputBorder="false" :placeholderStyle="placeholderStyle">
          </uni-easyinput>
        </uni-forms-item>
        <uni-forms-item name="age" label="年龄" required>
          <uni-easyinput v-model="state.model.age" type="number" maxlength="3" :disabled="isEdit" :trim="true"
            placeholder="年龄(岁)" :inputBorder="false" :placeholderStyle="placeholderStyle" />
        </uni-forms-item>

        <!-- 当前社交平台的绑定关系，只处理 wechat 微信场景 -->
        <view class="title-box ss-p-l-30">以上身份证信息主要用于学员购买保险</view>

        <uni-forms-item name="height" label="身高">
          <uni-easyinput v-model="state.model.height" type="number" maxlength="3" :disabled="isEdit" :trim="true"
            placeholder="身高(cm)" :inputBorder="false" :placeholderStyle="placeholderStyle" />
        </uni-forms-item>
        <uni-forms-item name="weight" label="体重">
          <uni-easyinput v-model="state.model.weight" type="number" maxlength="3" :disabled="isEdit" :trim="true"
            placeholder="体重(kg)" :inputBorder="false" :placeholderStyle="placeholderStyle" />
        </uni-forms-item>

        <uni-forms-item name="isDisease" label="是否有疾病史">
          <view class="ss-flex ss-col-center ss-h-100">
            <radio-group @change="onChangeDisease" class="ss-flex ss-col-center">
              <label class="radio" v-for="item in trueFalseMap" :key="item.value">
                <view class="ss-flex ss-col-center ss-m-r-32">
                  <radio :value="item.value" color="var(--ui-BG-Main)" style="transform: scale(0.8)" :disabled="isEdit"
                    :checked="parseInt(item.value) === state.model.isDisease" />
                  <view class="gender-name">{{ item.name }}</view>
                </view>
              </label>
            </radio-group>
          </view>
        </uni-forms-item>
      </view>
    </uni-forms>

    <su-fixed bottom placeholder bg="none" v-if="!isEdit">
      <view class="footer-box ss-p-20">
        <button class="ss-rest-button logout-btn ui-Shadow-Main" @tap="onSubmit">保存</button>
      </view>
    </su-fixed>
  </s-layout>
</template>

<script setup>
  import {
    computed,
    ref,
    reactive,
    onBeforeMount
  } from 'vue';
  import {
    onLoad
  } from '@dcloudio/uni-app';
  import sheep from '@/sheep';
  import {
    clone
  } from 'lodash-es';
  import {
    showAuthModal
  } from '@/sheep/hooks/useModal';
  import FileApi from '@/sheep/api/infra/file';
  import StudentApi from '@/sheep/api/member/student';
  import test from '@/sheep/helper/test.js';

  let isEdit = ref(true)
  const state = reactive({
    model: {
      sex: 1,
      isDisease: 0,
      avatar: 'https://txz-1332973494.cos.ap-guangzhou.myqcloud.com/static/img/shop/txz.jpg'
    }, // 个人信息
    rules: {},
    thirdInfo: {}, // 社交用户的信息
  });

  const placeholderStyle = 'color:#BBBBBB;font-size:28rpx;line-height:normal';

  const sexRadioMap = [{
      name: '男',
      value: '1',
    },
    {
      name: '女',
      value: '2',
    },
  ];
  const trueFalseMap = [{
      name: '是',
      value: '1',
    },
    {
      name: '否',
      value: '0',
    },
  ];

  // 选择性别
  function onChangeGender(e) {
    state.model.sex = e.detail.value;
  }
  // 选择性别
  function onChangeDisease(e) {
    state.model.isDisease = e.detail.value;
  }


  // 修改手机号
  const onChangeMobile = () => {
    showAuthModal('changeMobile');
  };

  // 选择微信的头像，进行上传
  function onChooseAvatar(e) {
    const tempUrl = e.detail.avatarUrl || '';
    uploadAvatar(tempUrl);
  }

  // 手动选择头像，进行上传
  function onChangeAvatar() {
    uni.chooseImage({
      success: async (chooseImageRes) => {
        const tempUrl = chooseImageRes.tempFilePaths[0];
        await uploadAvatar(tempUrl);
      },
    });
  }

  // 上传头像文件
  async function uploadAvatar(tempUrl) {
    if (!tempUrl) {
      return;
    }
    let {
      data
    } = await FileApi.uploadFile(tempUrl);
    state.model.avatar = data;
  }

  // 保存信息
  async function onSubmit() {
    console.log(state.model.name)
    if (state.model.name == null || !/^[\u4e00-\u9fa5\w]{2,16}$/.test(state.model.name)) {
      sheep.$helper.toast('请填写学员真实姓名');
      return;
    }
    if (state.model.idCard == null || !test.idCard(state.model.idCard)) {
      sheep.$helper.toast('请填写学员真实身份证号');
      return;
    }
    if (!/^1(3|4|5|7|8|9|6)\d{9}$/.test(state.model.mobile)) {
      sheep.$helper.toast('请填写正确的手机号');
      return;
    }
    // if (state.model.height == null || !test.digits(state.model.height)) {
    //   sheep.$helper.toast('请填写学员真实身高');
    //   return;
    // }
    // if (state.model.weight == null || !test.digits(state.model.weight)) {
    //   sheep.$helper.toast('请填写学员真实体重');
    //   return;
    // }
    if (state.model.age == null || !test.digits(state.model.age)) {
      sheep.$helper.toast('请填写学员真实年龄');
      return;
    }

    let res
    if (!!state.model.id) {
      res = await StudentApi.updateStudent(state.model);
    } else {
      res = await StudentApi.createStudent(state.model);
    }
    if (res.code === 0) {
      uni.showToast({
        icon: 'none',
        title: '保存成功',
      })
      sheep.$router.back()
    }
  }

  // 获得用户信息
  const getStudentInfo = async (id) => {
    // 个人信息
    if (!!id) {
      StudentApi.getStudentInfo({
        id
      }).then(res => {
        console.log(res)
        state.model = res.data;
      })
    }

  };

  onLoad((options) => {
    if (options.isEdit) {
      isEdit.value = Boolean(options.isEdit)
    }
    getStudentInfo(options.id);
  });
</script>

<style lang="scss" scoped>
  :deep() {
    .uni-file-picker {
      border-radius: 50%;
    }

    .uni-file-picker__container {
      margin: -14rpx -12rpx;
    }

    .file-picker__progress {
      height: 0 !important;
    }

    .uni-list-item__content-title {
      font-size: 28rpx !important;
      color: #333333 !important;
      line-height: normal !important;
    }

    .uni-icons {
      font-size: 40rpx !important;
    }

    .is-disabled {
      color: #333333;
    }
  }

  :deep(.disabled) {
    opacity: 1;
  }

  .gender-name {
    font-size: 28rpx;
    font-weight: 500;
    line-height: normal;
    color: #333333;
  }

  .title-box {
    font-size: 28rpx;
    font-weight: 500;
    color: #666666;
    line-height: 100rpx;
  }

  .logout-btn {
    width: 710rpx;
    height: 80rpx;
    background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
    border-radius: 40rpx;
    font-size: 30rpx;
    font-weight: 500;
    color: $white;
  }

  .radio-dark {
    filter: grayscale(100%);
    filter: gray;
    opacity: 0.4;
  }

  .content-img {
    border-radius: 50%;
  }

  .header-box-content {
    position: relative;
    width: 160rpx;
    height: 160rpx;
    overflow: hidden;
    border-radius: 50%;
  }

  .avatar-action {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    bottom: 0;
    z-index: 1;
    width: 160rpx;
    height: 46rpx;
    background: rgba(#000000, 0.3);

    .avatar-action-btn {
      width: 160rpx;
      height: 46rpx;
      font-weight: 500;
      font-size: 24rpx;
      color: #ffffff;
    }
  }

  // 绑定项
  .account-list {
    background-color: $white;
    height: 100rpx;
    padding: 0 20rpx;

    .list-img {
      width: 40rpx;
      height: 40rpx;
      margin-right: 10rpx;
    }

    .list-name {
      font-size: 28rpx;
      color: #333333;
    }

    .info {
      .avatar {
        width: 38rpx;
        height: 38rpx;
        border-radius: 50%;
        overflow: hidden;
      }

      .name {
        font-size: 28rpx;
        font-weight: 400;
        color: $dark-9;
      }
    }

    .bind-box {
      width: 100rpx;
      height: 50rpx;
      line-height: normal;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 24rpx;

      .bind-btn {
        width: 100%;
        height: 100%;
        border-radius: 25rpx;
        background: #f4f4f4;
        color: #999999;
      }

      .relieve-btn {
        width: 100%;
        height: 100%;
        border-radius: 25rpx;
        background: var(--ui-BG-Main-opacity-1);
        color: var(--ui-BG-Main);
      }
    }
  }

  .list-border {
    font-size: 28rpx;
    font-weight: 400;
    color: #333333;
    border-bottom: 2rpx solid #eeeeee;
  }

  image {
    width: 100%;
    height: 100%;
  }
</style>